//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#ifndef _OBJECTVIEW_H
#define _OBJECTVIEW_H
//
#include <common/identified.h>
#include <model/particletemplate.h>
#include <model/emittertemplate.h>
#include <model/group.h>
#include "attrevent.h"
#include "viewattributes.h"
#include "view.h"
//
template<class T>
class ObjectView:public View
{
   Q_DISABLE_COPY(ObjectView<T>)
//construction
public:
   ObjectView()
   :
      object(0)
   {;}
//View
protected:
   void handle_set_attr_group()
   {
      if(get_attr_group())
         set_object(static_cast<T*>(ViewAttributeType::ModelDataObject::lookup(get_attr_group(),root_object)->get_object()));
      else
         set_object(0);
   }
   void handle_attr_event(AttrEvent *evnt)
   {
      if(!get_document())
         return;
      switch(evnt->get_type())
      {
      case AttrEvent::value_changed:
         {
            AttrValueChanged *avc=static_cast<AttrValueChanged*>(evnt);
            if(avc->get_name()==root_object)
            {
               set_object(static_cast<T*>(ViewAttributeType::ModelDataObject::lookup(get_attr_group(),root_object)->get_object()));
            }
         }
         break;
      }
   }
//implementation
protected:
   virtual void handle_object_changed(T *old_object)
   {
   }
   T *get_object() const
   {
      return object;
   }
   void set_object(T *object)
   {
      T * const old_object=this->object;
      this->object=object;
      handle_object_changed(old_object);
   }
private:
   T *object;
};
//
typedef ObjectView<Identified> DataObjectView;
typedef ObjectView<model::ParticleTemplate> ParticleTemplateView;
typedef ObjectView<model::EmitterTemplate> EmitterTemplateView;
typedef ObjectView<model::Group> GroupView;
//
#endif
